<%@ page language="java" pageEncoding="utf-8"
	contentType="text/html; charset=utf-8"%>
<%@ include file="../include/taglib.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>添加优惠信息</title>
		<%@ include file="../include/jsCss.jsp"%>
		<script type="text/javascript" src="${webUrl}/js/My97DatePicker/WdatePicker.js"></script>
	</head>

	<body>
		<form action="preferential!preferentialSaveOrUpdate.html"  method="post" id="preferentialForm">
			<table border="1"> 
				<tr>
					<td colspan="2">
						添加优惠信息
					</td>
				</tr>
				<tr>
					<td>上传图片</td>
					<td><iframe src="${webUrl}/images!imageUpload2.html?width=40&height=40" width="100%" height="250" scrolling="no" frameborder="0" style="border: 0px;"></iframe> </td>
				</tr>
				<tr>
					<td>
						优惠名称
					</td>
					<td>
						<input type="text" name="name" id="name" value="${preferentialDto.name}" onblur="javascript:valName();"/>
						<span id="nameShow"></span>
						<input type="hidden" name="id" value="${preferentialDto.id }"/>
					</td>
				</tr>
				<tr>
					<td colspan="2">
						有套餐  
						<input id="packageCheckBox" type="checkbox" onclick="preferentialPages(this);"  <s:if test="preferentialDto.mealMenuId != null && preferentialDto.mealMenuId != ''">checked='checked'</s:if>/>
					</td>
				</tr>
				<tr id="preferentialTrId" style="display: none;">
					<td>
						优惠套餐
					</td>
					<td>
						<div id="preferentialPackages"></div>
						<a href="javascript:;" onclick="editPreferentialPages();">编辑</a>
						<input type="hidden" name="packageRemark" id="packageRemark"/>
						<input type="hidden" name="mealMenuId" id="mealMenuId"/>
					</td>
				</tr>
				<tr>
					<td colspan="2">
						原价 ：
						<input type="text" name="costPrice" id="castPrice" size="5" value="${preferentialDto.costPrice }" onblur="javascript:iscostPriceNumber();"/>
						<span id="isCostPrice"></span>
						优惠价格：
						<input type="text" name="preferentialPrice" id="preferentialPrice" size="5" value="${preferentialDto.preferentialPrice }" onblur="javascript:isPreferentialPrice();"/>
						<span id="isPreferPrice"></span>
						折扣幅度：
						<input type="text" name="discount" id="discount" size="5" value="${preferentialDto.discount}" /> 
					</td>
				</tr>
				<tr>
					<td>
						优惠期限
					</td>
					<td>
						<%-- <input type="text" name="beginDate" value="${preferentialDto.beginDate }"/> --%>
						<input type="textbox" size="18" onClick="WdatePicker();" name="beginDate" value="${preferentialDto.beginDate}" onFocus="WdatePicker({startDate:'00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})"/>
						至
						<%--<input type="text" name="endDate" value="${preferentialDto.endDate }"/>--%>
						<input type="textbox" size="18" onClick="WdatePicker();" name="endDate" value="${preferentialDto.endDate}" onFocus="WdatePicker({startDate:'00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})"/>
					</td>
				</tr>
				<tr>
					<td>
						优惠份数
					</td>
					<td>
						<input type="text" name="copies" id="copies" value="${preferentialDto.copies }" onblur="javascript:isCopiesNumber();"/>
						<span id="copiesNumber"></span>
					</td>
				</tr>
				<tr>
					<td>
						优惠说明
					</td>
					<td>
						<textarea rows="5" cols="25" name="explanation">${preferentialDto.explanation }</textarea>
					</td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="button"  value="提交" onclick="preferentialSubmit();"/>
						&nbsp;&nbsp;
						<input type="reset" value="重置" />
					</td>
				</tr>
			</table>
		</form>
		<div id="pageAllCover" style="overflow: hidden; width: 100%; height: 100%; border: 0px none; padding: 0px; margin: 0px; top: 0px; left: 0px; display: none; visibility: visible; background-color: rgb(255, 255, 255); opacity: 0.5; position: fixed; z-index: 100;"></div>
		<div id="optiontransferselectId" style="top: 20%; left: 30%; display: none; visibility: visible; position: absolute; z-index: 1000;background: white;" >
				<s:optiontransferselect name="left" list="#request.leftList"
								listKey="id" listValue="menuName" doubleList="rightList" doubleName="right"
								doubleListKey="id" doubleListValue="menuName" addToLeftLabel="删除菜肴"
								addAllToRightLabel="添加全部菜肴" addAllToLeftLabel="删除全部菜肴"
								addToRightLabel="添加菜肴"></s:optiontransferselect>
				<input type="button" onclick="addPreferential()" value="提交"/> 
				<input type="button" onclick="closePreferential()" value="关闭" />
		</div>
		
		<script>
			function closePreferential() {
				$("#pageAllCover").css("display","none");
				$("#optiontransferselectId").css("display","none");
			}
			
			function editPreferentialPages() {
				$("#optiontransferselectId").css("display","block");
			}
			function preferentialPages(t) {
				if (t.checked == true) {
					//$("#pageAllCover").css("display","block");
					$("#optiontransferselectId").css("display","block");	
				} else {
					//$("#pageAllCover").css("display","none");
					$("#optiontransferselectId").css("display","none");
					$("#preferentialTrId").css("display","none");
				}
				
			}
			
			function addPreferential() {
				var rightLength = document.getElementById('right').options;
				var addHtml = "<table border='1' ><tr><td>菜肴名称(描述)</td><td>价格</td></tr>";
				var packageRemark  = '${preferentialDto.packageRemark}'.split(",");
				
				for (var k = 0;k < rightLength.length;k++) {
					<s:iterator value="mealMenuList">  
						if (rightLength[k].value == ${id}) { 
							var remark = ""; 
							for (var n = 0;n < packageRemark.length; n++) {
								var remarkInfo =  packageRemark[n].split('-'); 
								if (remarkInfo[1] == ${id}) {
									remark = remarkInfo[0];
								}
							}
							addHtml += "<tr><td>${menuName}<input type='text' size='5' name='remarkInfo' alt='${id}' value='"+remark+"'/> </td> <td>${price}</td></tr>";
						} 
					</s:iterator> 
				}
				addHtml += "</table>";
				$("#preferentialPackages").html(addHtml);
				$("#pageAllCover").css("display","none");
				$("#optiontransferselectId").css("display","none");
				$("#preferentialTrId").css("display","block");
				
			} 
			
			function preferentialSubmit() {
				var remarkInfo = "";
				var mealMenuId = "";
				if ($("#packageCheckBox").attr("checked") == true) {
					$("input[name='remarkInfo']").each(function(i,item){
						if ($.trim(item.value) != "") {
							remarkInfo += item.value + "-" + item.alt + ",";
						}
						mealMenuId += item.alt + ",";
					});
					if (remarkInfo != "") {
						remarkInfo = remarkInfo.substring(0, remarkInfo.length-1);
					}
					if (mealMenuId != "") {
						mealMenuId = mealMenuId.substring(0, mealMenuId.length-1);
					}
					$("#packageRemark").val(remarkInfo);
					$("#mealMenuId").val(mealMenuId);
				} else {
					$("#packageRemark").val('');
					$("#mealMenuId").val('');
				}
				
				//判断是否为合法输入
				var castPrices = $("#castPrice").val();//原价
				var preferentialPrices = $("#preferentialPrice").val();//优惠价
				var copes = $("#copies").val();//优惠份数
				var name = $("#name").val();
				
				//优惠名称
				if($.trim(name) == ''){
					$("#nameShow").show();
					$("#nameShow").text("请输入优惠名称");
					$("#nameShow").css('color','red');
					return;
				}else{
					$("#nameShow").hide();
				}
			
				//原价
				if($.trim(castPrices) == ''){
					$("#isCostPrice").show();
					$("#isCostPrice").text("请输入原价");
					$("#isCostPrice").css('color','red');
					return;
				}else{
					if(isNaN(castPrices)){
						$("#isCostPrice").show();
						$("#isCostPrice").text("请输入数字");
						$("#isCostPrice").css('color','red');
						return;
					}else{
						$("#isCostPrice").hide();
					}
				}
				
				//优惠价格
				if($.trim(preferentialPrices) == ''){
					$("#isPreferPrice").show();
					$("#isPreferPrice").text("请输入优惠价格");
					$("#isPreferPrice").css('color','red');
					return;
				}else{
					if(isNaN(preferentialPrices)){
						$("#isPreferPrice").show();
						$("#isPreferPrice").text("请输入数字");
						$("#isPreferPrice").css('color','red');
						return;
					}else{
						$("#isPreferPrice").hide();
					}
				}
				
				//份数
				if($.trim(copes) == ''){
					$("#copiesNumber").show();
					$("#copiesNumber").text("请输入优惠份数");
					$("#copiesNumber").css('color','red');
					return;
				}else{
					if(isNaN(copes)){
						$("#copiesNumber").show();
						$("#copiesNumber").text("请输入数字");
						$("#copiesNumber").css('color','red');
						return;
					}else{
						$("#copiesNumber").hide();
					}
				}
				
				$("#preferentialForm").submit();//提交
			}
			
			<s:if test="preferentialDto.mealMenuId != null && preferentialDto.mealMenuId != ''">
				addPreferential()
			</s:if>
		$("#optiontransferselectId table").attr("border","1");
		
		//优惠名称
		function valName(){
			var name = $("#name").val();
			
			if($.trim(name) == ''){
				$("#nameShow").show();
				$("#nameShow").text("请输入优惠名称");
				$("#nameShow").css('color','red');
			}else{
				$("#nameShow").hide();
			}
		}
		
		//判断输入是否为数字
		function iscostPriceNumber(){
			var castPrices = $("#castPrice").val();
			
			if($.trim(castPrices) == ''){
				$("#isCostPrice").show();
				$("#isCostPrice").text("请输入原价");
				$("#isCostPrice").css('color','red');
				return;
			}else{
				if(isNaN(castPrices)){
					$("#isCostPrice").show();
					$("#isCostPrice").text("请输入数字");
					$("#isCostPrice").css('color','red');
					return;
				}else{
					$("#isCostPrice").hide();
				}
			}
				
		}
		
		function isPreferentialPrice(){
			var castPrices = $("#castPrice").val();
			var preferentialPrices = $("#preferentialPrice").val();
			
			
			if($.trim(preferentialPrices) == ''){
				$("#isPreferPrice").show();
				$("#isPreferPrice").text("请输入优惠价格");
				$("#isPreferPrice").css('color','red');
				return;
			}else{
				if(isNaN(preferentialPrices)){
					$("#isPreferPrice").show();
					$("#isPreferPrice").text("请输入数字");
					$("#isPreferPrice").css('color','red');
					return;
				}else{
					$("#isPreferPrice").hide();
				}
			}
			
			var count = preferentialPrices/castPrices;
			document.getElementById("discount").value = parseFloat(count).toFixed(1);
		}
		
		//优惠份数
		function isCopiesNumber(){
			var copes = $("#copies").val();
			
			if($.trim(copes) == ''){
				$("#copiesNumber").show();
				$("#copiesNumber").text("请输入优惠份数");
				$("#copiesNumber").css('color','red');
			}else{
				if(isNaN(copes)){
					$("#copiesNumber").show();
					$("#copiesNumber").text("请输入数字");
					$("#copiesNumber").css('color','red');
					return;
				}else{
					$("#copiesNumber").hide();
				}
			}
		}
		</script>
	</body>
</html>